package com.dearbinge.opentapi.content.thumbs;

import com.dearbinge.data.api.thumbs.ThumbsService;
import com.dearbinge.data.pojo.thumbs.Thumbs;
import com.dearbinge.data.pojo.thumbs.vo.ThumbsResult;
import com.dearbinge.utils.ResultMsg;
import com.dearbinge.utils.enums.ResultStatusCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.List;
import java.util.Map;

@Api(description = "c：点赞接口")
@RestController
@RequestMapping("/thumbs")
@EnableAutoConfiguration
public class ThumbsController extends WebMvcConfigurerAdapter {

    @Autowired
    private ThumbsService thumbsService;


    /**
     * 新增/修改
     */
    @ApiOperation(value = "新增/修改",notes = "新增/修改")
    @RequestMapping(value = "/addOrUpdate",method = RequestMethod.POST)
    public ResultMsg<String> addOrUpdate(@RequestBody Thumbs thumbs){
        Integer dataCode = 0;
        try {
            if (thumbs.getId() != null && thumbs.getId() != 0){
                dataCode = thumbsService.update(thumbs);
                if (dataCode > 0){
                    return ResultMsg.ok("修改成功");
                }
            }else {
                Thumbs thumbss = thumbsService.getIsThumbs(thumbs.getIsThumbsId(),thumbs.getThumbsType(),thumbs.getUserId());
                if (thumbss != null){
                    return ResultMsg.error(500,"已点赞，不能重复点赞！");
                }else if(thumbss == null){
                    String code = thumbsService.insert(thumbs);
                    String[] split = code.split("-");
                    if (split.length > 1){
                        return ResultMsg.error(201,split[1]);
                    }else {
                        if (code.equals("1")){
                            return ResultMsg.ok("新增成功");
                        }else if (code.equals("501")){
                            return ResultMsg.error(501,"红包已抢完");
                        }
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

    /**
     * 删除
     */
    @ApiOperation(value = "根据ID删除",notes = "根据ID删除")
    @ApiImplicitParam(name = "id",value = "id",paramType = "query")
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public ResultMsg<String> delete(Integer id){
        try {
            Integer dataCode = thumbsService.delete(id);
            if (dataCode > 0){
                return ResultMsg.ok("删除成功");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

    /**
     * 根据ID查询点赞数据
     */
    @ApiOperation(value = "根据ID查询点赞数据",notes = "根据ID查询点赞数据")
    @ApiImplicitParam(name = "id",value = "id",paramType = "query")
    @RequestMapping(value = "/getById",method = RequestMethod.GET)
     public ResultMsg<Map<String,Object>> getById(Integer id){
        try {
            Map<String,Object> thumbs = thumbsService.getById(id);
            return ResultMsg.ok(thumbs);
        }catch (Exception e){
            e.printStackTrace();
        }
         return ResultMsg.error(500,"系统异常");
     }

    /**
     * 根据被点赞的id与分类查询全部的点赞数据
     */
    @ApiOperation(value = "根据ID与分类查询全部的点赞数据",notes = "根据ID与分类查询全部的点赞数据")
    @ApiImplicitParams({@ApiImplicitParam(name = "isThumbsId",value = "被点赞数据ID",paramType = "query"),
            @ApiImplicitParam(name = "thumbsType", value = "点赞类型（1：生活   2：商品  3：秀场）", paramType = "query")})
    @RequestMapping(value = "/getByIdAndThumbsType",method = RequestMethod.GET)
    public ResultMsg<List<Map<String,Object>>> getByIdAndThumbsType(String isThumbsId,Integer thumbsType){
        try {
            List<Map<String,Object>> list = thumbsService.getByIdAndThumbsType(isThumbsId,thumbsType);
            return ResultMsg.ok(list);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

    /**
     * 根据ID与分类查询全部的点赞数据和用户信息
     */
    @ApiOperation(value = "根据ID与分类查询全部的点赞数据和用户信息", notes = "根据ID与分类查询全部的点赞数据和用户信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "isThumbsId", value = "被点赞数据ID", paramType = "query"),
            @ApiImplicitParam(name = "thumbsType", value = "点赞类型（1：生活   2：商品  3：秀场）", paramType = "query")})
    @RequestMapping(value = "/listThumbsAndUser", method = RequestMethod.GET)
    public ResultMsg<List<ThumbsResult>> listThumbsAndUser(String isThumbsId, Integer thumbsType) {
        try {
            List<ThumbsResult> list = thumbsService.listThumbsAndUser(isThumbsId, thumbsType);
            return ResultMsg.okData(list, ResultStatusCodeEnum.SUCCESS.getDesc());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultMsg.error(ResultStatusCodeEnum.CODE500.getCode(), ResultStatusCodeEnum.CODE500.getDesc());
    }
    /**
     * 根据用户ID与被点赞ID和分类查询该用户是否点赞
     */
    @ApiOperation(value = "根据用户ID与被点赞ID和分类查询该用户是否点赞",notes = "根据用户ID与被点赞ID和分类查询该用户是否点赞")
    @ApiImplicitParams({@ApiImplicitParam(name = "isThumbsId",value = "被点赞数据ID",paramType = "query"),
            @ApiImplicitParam(name = "thumbsType", value = "点赞类型（1：生活   2：商品  3：秀场）", paramType = "query"),
            @ApiImplicitParam(name = "userId",value = "用户ID",paramType = "query")})
    @RequestMapping(value = "/getIsThumbs",method = RequestMethod.GET)
    public ResultMsg<Boolean> getIsThumbs(String isThumbsId, Integer thumbsType,String userId){
        try {
            Thumbs thumbs = thumbsService.getIsThumbs(isThumbsId,thumbsType,userId);
            if (thumbs != null){
                return ResultMsg.ok(true);
            }
            return ResultMsg.ok(false);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

}
